
#include "dvr_cmd.h"
#include "storage_api.h"
//#include "dvr_mml_svr.h"
#include "cross_com.h"
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
//#include "RTSPServer_APP.h"
#else
//#include "resource.h"
//#include "MultiCHData.h"
#endif // _WIN32
//#include "dvr_net_app.h"
#include "dvr_mml_svr.h"
//#include "lua_app.h"
//CLuaApp* g_lua_app = new CLuaApp();

extern CStreamerSvr g_stream_server;
//extern CDVR_APP g_dvrApp;
CDVRCmd::CDVRCmd()
{
	m_sbuf = NULL;
	m_fInfoManger.Init(FILE_MAX_CNT);
	m_fd = 0;
	m_clientID = 0;
	m_pClient_addr = NULL;
	m_client_len = 0;
}

CDVRCmd::~CDVRCmd()
{
	if(NULL != m_sbuf)
	{
		delete [] m_sbuf;
	}

}

int CDVRCmd::Init(SOCKET fd,int clientID)
{
	int ret = 0;
	m_fd = fd;
	m_heat_last = 0;
	if(m_sbuf == NULL)
	{
		m_sbuf_sz = SIZE_KB(64);
		m_sbuf = new char[m_sbuf_sz];
	}
	m_clientID = clientID;
	return ret;
}

int CDVRCmd::AttachSocket(SOCKET fd_svr, struct sockaddr* client_addr, int client_len)
{
	m_fd = fd_svr;
	m_pClient_addr = client_addr;
	m_client_len = client_len;
	m_bUdp = true;
	return 0;
}

int CDVRCmd::Destroy()
{
	int ret = 0;
	if(NULL != m_sbuf)
	{
		delete [] m_sbuf;
	}

	return ret;
}


int CDVRCmd::AnalysisCmd(unsigned char * cmd_buf,int cmd_len)
{
	int ret = 0;
	DVR_APP_HEAD * pApp = (DVR_APP_HEAD * )cmd_buf;

	if(pApp->cmd_main != 0 && pApp->cmd_sub != 0)
	{
		TRACE("Recive main cmd %d sub cmd %d  %d cmd_len.\n",pApp->cmd_main,pApp->cmd_sub,cmd_len);
	}

	char sub_cmd = pApp->cmd_sub;
	unsigned char * pData = cmd_buf + sizeof(DVR_APP_HEAD);
	int data_len = pApp->dataLen - 2;

	if(pApp->appFlag != DVR_APP_TRANS_FLAG )
	{
		return -1;
	}

	//CRCУ??
// 	ret = CheckCRC32(cmd_buf,cmd_len);
// 	if(ret < 0)
// 	{
// 		return ERR_CRC32;
// 	}

	switch (pApp->cmd_main)
	{
		//???????
	case CONNECTION_CMD:
		ret = ConnectProc(sub_cmd,pData,data_len);
		break;
		//UI????
	case UI_CONTROL_CMD:
		ret = UICtrlProc(sub_cmd,pData,data_len);
		break;

		//???????
	case FAST_CONTROL_CMD:
		ret = FastCtrlProc(sub_cmd,pData,data_len);
		break;

	case FILE_CONTROL_CMD:
		ret = FileCtrlProc(sub_cmd,pData,data_len);
		break;

	case CONFIG_GET_CMD:
		ret = CfgGetProc(sub_cmd,pData,data_len);
		break;

	case CONFIG_SET_CMD:
		ret = CfgSetProc(sub_cmd,pData,data_len);
		break;

	case PLAYBACK_CMD:
		ret = ReplayCtrlProc(sub_cmd,pData,data_len);
		break;
	case LIVE_CTRL:
		ret = LiveCtrlProc(sub_cmd,pData,data_len);
		break;
	case DISK_MANAGE_CMD:
		ret = DiskMangerProc(sub_cmd,pData,data_len);
		break;
	//case LUA_RUN:
	//	ret = LuaRunProc(sub_cmd, pData, data_len);
	//	break;
	default:
		break;
	}

	return ret;
}

uint32_t CDVRCmd::GetCRC32_FromBuf(unsigned char * cmd_buf,uint32_t data_sz)
{
	DVR_APP_HEAD * pApp = (DVR_APP_HEAD * )cmd_buf;
	uint32_t crc_cmd = 0;

	unsigned char * crc_data = cmd_buf + 8 + pApp->dataLen;
	if(pApp->dataLen > data_sz)
	{
		printf("head info datalen error.data size %d \n",data_sz);
		return 0;
	}

	crc_cmd =   (uint32_t)crc_data[0]| (uint32_t)crc_data[1]<<8|
				(uint32_t)crc_data[2]<<16|
				(uint32_t)crc_data[3]<<24;

	return crc_cmd;
}

int CDVRCmd::CheckCRC32(unsigned char * cmd_buf,uint32_t len)
{
	uint32_t cnt_crc    = 0;
	uint32_t trans_crc  = 0;
	DVR_APP_HEAD * pApp = (DVR_APP_HEAD * )cmd_buf;

	if(pApp->dataLen +4  > len)
	{
		return ERR_DATA_AREA;
	}

	//????????CRC
	//cnt_crc = lhn_crc32(cmd_buf +2, pApp->dataLen+ 6);

	//??????????????CRC
	//trans_crc = GetCRC32_FromBuf(cmd_buf,len);

	//У???CRC32?????????????????????????????
	if(0)
	{
		if(cnt_crc != trans_crc)
		{
			TRACE("ERROR CRC32 re count %d transfer crc %d .\n",cnt_crc,trans_crc);
			return -1;
		}
	}

	return 0;
}

int CDVRCmd::RespondDataPkt(char * buf, int data_sz)
{
	int ret  = 0;
	if (m_bUdp)
	{
		ret = sendto(m_fd, buf, data_sz, 0, m_pClient_addr, m_client_len);
	}
	else
	{
		ret = send(m_fd, buf, data_sz, 0);
	}

	if(ret < 0)
	{
        int err = 0;
#ifdef _WIN32
        err = GetLastError();
#else
        err = errno;
#endif
		TRACE("Send error m_fd %d  data_sz %d ret %d errno %d .\n",m_fd,data_sz,ret,err);
	}

	return ret;
}

//client App
int CDVRCmd::SendPkt(char * buf, int data_sz,char cmd_main,char cmd_sub)
{
	int ret  = 0;
	int out_len = 0;
	uint32_t crc32 = 0;
	int      crc_chk_len = 0;
	unsigned char  *  pCrcBuf = NULL;
	char  *  pCrcValue = NULL;

	DVR_APP_HEAD * pApp_head = (DVR_APP_HEAD*)m_sbuf;

	pApp_head->appFlag = DVR_APP_TRANS_FLAG;
	pApp_head->dataLen = data_sz + 2;
	pApp_head->cmd_main= cmd_main;
	pApp_head->cmd_sub = cmd_sub;

	out_len = DVR_APP_DATA_POS + data_sz + 4;   //???8????DVR???β??4??????CRC32

	memcpy(m_sbuf + DVR_APP_DATA_POS,buf,data_sz);

	pCrcBuf = (unsigned char*)(m_sbuf + 2);
	crc_chk_len =  6 + data_sz;
	crc32 = 0;//lhn_crc32(pCrcBuf,crc_chk_len);

	pCrcValue = m_sbuf + DVR_APP_DATA_POS + data_sz;

	//С???????
	pCrcValue[0] = crc32     & 0xFF;
	pCrcValue[1] = crc32>>8  & 0xFF;
	pCrcValue[2] = crc32>>16 & 0xFF;
	pCrcValue[3] = crc32>>24 & 0xFF;

	if (m_bUdp)
	{
		ret = sendto(m_fd, m_sbuf, out_len, 0, m_pClient_addr, m_client_len);
	}
	else
	{
		ret = send(m_fd, m_sbuf, out_len, 0);
		if (ret < 0)
		{
			TRACE_ERR("Send error data_sz %d ret %d  .\n", data_sz, ret);
		}
	}

	return ret;
}

int CDVRCmd::ConnectProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	int send_len = 0;
	uint8_t TpVersion[4] = {0};
	uint8_t MacAddress[6] = {0};       //DVR??MAC???
	uint8_t MagicWord[10] = {0};

	switch (sub_cmd)
	{
	case  CONN_SUB_HANDSHAKE://            0x00

		//V01.01
		TpVersion[0] = 0;
		TpVersion[1] = 1;
		TpVersion[2] = 0;
		TpVersion[3] = 1;

		send_len = 4;
		ret = RespondDataPkt((char *)TpVersion,send_len);

		break;

	case CONN_SUB_PAIRING_REQS://           0x01


		send_len = 6;
		ret = RespondDataPkt((char *)MacAddress,send_len);

		break;

	case CONN_SUB_PAIRING_CHECK:

		send_len = 10;
		ret = RespondDataPkt((char *)MagicWord,send_len);

		break;

	case CONN_SUB_HEARTBEAT:       //0x10
		send_len = sizeof(HEART_BEAT_RET);
		memset(&m_beart,0,sizeof(HEART_BEAT_REQ));

		//STORAGE_GET_STORAGE_STAT((StorageStat*)&m_beart);
		m_beart.UIStatus           = 0;
		m_beart.home               = 0;

		ret = RespondDataPkt((char *)&m_beart,send_len);
#ifdef _WIN32
		TRACE("hear info  cycle %d  sdcard %d  send_len %d  .\n",m_beart.CycleRec,m_beart.sdcordStat,send_len);
#endif
		break;

	case CONN_SUB_AP_CONNECT_REQS:
		//???????
		break;
	default:
		break;
	}

	return ret;
}

int CDVRCmd::UICtrlProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{   //??????
	int ret = 0;
	switch (sub_cmd)
	{
	case UI_CTL_REQ_PREVIEW:

		break;

	case UI_CTL_REQ_FILES:

		break;

	case UI_CTL_REQ_CONFIG:

		break;
	case  UI_CTL_REQ_PLAYBACK:

		break;
	default:
		break;
	}
	return ret;
}

int CDVRCmd::FastCtrlProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	int runRet        = 0;  //??н??
	uint8_t CycleFlag = 0;
	time_t sysTm = 0;
	int time_zone = 0;
	char tmp_buf[40] = {0};
    int ch = 0;
	REC_INFO * pRecInfo = (REC_INFO *)data_buf;
#ifdef _WIN32
	HWND hWnd  = NULL;
#endif
#ifndef _TEST_NETWOOK_
	//CMultiCHData* pMulData = CMultiCHData::getSingleton();
	switch (sub_cmd)
	{
	case FAST_CTL_CYCLE_RECORD:

		CycleFlag = data_buf[0];

		runRet = STFS_CycleRec((int)CycleFlag);

		ret = RespondDataPkt((char *)&runRet,4);

		break;

	case FAST_CTL_EMERGE:
		//??????????
		//runRet = START_EVT_RECORD(0);
		//???????
		ret = RespondDataPkt((char *)&runRet,4);

		break;

	case FAST_CTL_PHOTOGRAPHY:
		//????
#ifdef _WIN32

// 		hWnd =  pMulData->GetMainWnd();
// 		if(hWnd != NULL)
// 		{
// 			::SendMessage(hWnd,WM_COMMAND,IDC_BTN_SNAP,NULL);
// 		}

#else
		//?????????????
	//	MML_SNAP_PIC(0,1);
#endif
		//???????
		ret = RespondDataPkt((char *)&runRet,4);

		break;

    case FAST_CTL_LIVE_CH:
        ch = data_buf[0];
   //    rtsp_live_switch_ch(ch);
        ret = RespondDataPkt((char *)&runRet,4);
        break;

	case FAST_CTL_SET_TIME:
#ifdef _WIN32

#else
		sysTm = *((time_t*)data_buf);
		stime(&sysTm);

		time_zone  = *(int*)(data_buf + 4);

		if(time_zone != 0 && time_zone < 24)
		{
			sprintf(tmp_buf,":GMT-%d",time_zone);
			setenv("TZ", tmp_buf, 1);
			tzset();
			TRACE("tzset zone is  %d %s \n",time_zone,tmp_buf);
		}

		TRACE("Time zone is %d dest time %d .\n",time_zone,sysTm);

#endif
		ret = RespondDataPkt((char *)&runRet,4);
		break;

	case FAST_CTL_REC_SWITCH:
		if(pRecInfo->isEnable)
		{
			ret = STFS_StartRecord(pRecInfo->channel,FS_RECORD_MANUAL,0,0,NULL);
		}
		else
		{
		    ret = STFS_ManualRecStop(pRecInfo->channel);
		}
		ret = RespondDataPkt((char *)&runRet,4);
		break;
	case FAST_CTL_REC_STOP:
		TRACE("Will call STORAGE_STOP_RECORD.\n");
		ret = STFS_RecorcStop();
		ret = RespondDataPkt((char *)&runRet,4);
		break;
	case FAST_CTL_EVENT_RECORD:
	{
		EVENT_RECROD* pEvt = (EVENT_RECROD*)data_buf;
		TRACE("Will call FAST_CTL_EVENT_RECORD.\n");
		STFS_StartRecord(pEvt->ch,(FS_REC_TYPE)pEvt->event_id,pEvt->prepare_sec,pEvt->keep_sec,NULL);
	}
	break;
	// case FAST_CTL_EVENT_RECORD_TAG:
	// {
	// 	EVENT_RECROD_TAG* pEvt = (EVENT_RECROD_TAG*)data_buf;
	// 	EXTRA_REC_INFO  einfo = {0};

	// 	memcpy(einfo.extra_info, pEvt->tag, pEvt->tag_len);
		
	// 	TRACE("ch %d evtid %d  tag event %s .\n", pEvt->ch, pEvt->event_id, einfo.extra_info);

	// 	STFS_StartRecord(pEvt->ch, (FS_REC_TYPE)pEvt->event_id, pEvt->prepare_sec, pEvt->keep_sec, &einfo);
	// }
	// case FAST_CTL_REC_SW:
	// {
	// 	bool bEnable = *((bool*)data_buf);
	// 	TRACE("STFS_RecordSw %d .\n",bEnable);
	// 	STFS_RecordSw(bEnable);
	// }
		break;
	default:
		break;
	}
#endif // _TEST_NETWOOK_
	return ret;
}

int CDVRCmd::FileCtrlProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	#if 0
	
	FILE_LST_REQ * pFileLstReq = NULL;
//	FILE_LST_TAG_REQ* pFileLstTAGReq = NULL;
	FILE_DEL_INFO_REQ * pDelFileLst = NULL;
	typedef struct _RECORD_INFO_
	{
		int disk;
		int blk_no;
		int seg_no;

	}RECORD_INFO_T;

	int send_len = 0;
	char buf[SIZE_4KB] = {0};
	int  buf_size = SIZE_4KB;
	char data[20] = {0};
	char * pDiskInfo = NULL;
	switch (sub_cmd)
	{
	case FILE_CTL_GET_DISK_INFO:
		TRACE("recive cmd Get disk info.\n");
		pDiskInfo = buf + 4;
		ret = STFS_GetDiskInfo(pDiskInfo,buf_size,NULL);
		if(ret > 0)
		{   //???????
			TRACE("Get file list. ret %d.\n",ret);
			uint32_t * pRet = (uint32_t*)buf;

			*pRet = ret;
			send_len = ret * sizeof(buf_size) + 4;
			ret = RespondDataPkt(buf,send_len);
		}
		else
		{   //???
			ret = RespondDataPkt(data,4);
			TRACE("Get file list fail. ret %d.\n",ret);
		}
		break;

	case FILE_CTL_GET_FILE_LIST:

		pFileLstReq = (FILE_LST_REQ*)data_buf;

		//获取文件列表
		ret = m_fInfoManger.GetFileLst(pFileLstReq,m_clientID);
		if(ret > 0)
		{   //成功时回复
			send_len = ret * sizeof(RECORD_SEGMENT) + 4;
			
			ret = RespondDataPkt(m_fInfoManger.GetLstBuf(),send_len);
			TRACE("RespondDataPkt. ret %d. send_len %d m_bUdp %d  \n",ret,send_len,m_bUdp);
		}
		else
		{   //???

			ret = RespondDataPkt(data,4);
			TRACE("Get file list fail. ret %d.\n",ret);
		}
		break;
	// case FILE_CTL_GET_FILE_LIST_TAG:
	// 	pFileLstTAGReq = (FILE_LST_TAG_REQ*)data_buf;

	// 	//获取文件列表
	// 	ret = m_fInfoManger.GetFileLstTag(pFileLstTAGReq, m_clientID);
	// 	if (ret > 0)
	// 	{   //成功时回复
	// 		send_len = ret * sizeof(FILE_LST_TAG_REQ) + 4;

	// 		ret = RespondDataPkt(m_fInfoManger.GetLstBuf(), send_len);
	// 		TRACE("RespondDataPkt. ret %d. send_len %d m_bUdp %d  \n", ret, send_len, m_bUdp);
	// 	}
	// 	else
	// 	{   //???

	// 		ret = RespondDataPkt(data, 4);
	// 		TRACE("Get file list fail. ret %d.\n", ret);
	// 	}
	// 	break;
		break;

	case 0x03://FILE_CTL_LOCK_FILE:
	{
		RECORD_INFO_T* pLock = (RECORD_INFO_T*)data_buf;
		RECORD_SEGMENT seg;
		memset(&seg,0,sizeof(RECORD_SEGMENT)); 

		seg.diskIndex = pLock->disk;
		seg.indexNO = pLock->blk_no;
		seg.recSegIndex = pLock->seg_no;
		printf("LOCK_FILE diskIndex %d indexNO %d recSegIndex %d .\n",
			seg.diskIndex, seg.indexNO, seg.recSegIndex);
		STFS_LockRecord(&seg);

		break;
	}
	case 0x04:// FILE_CTL_UNLOCK_FILE:
	{	
		RECORD_INFO_T* pLock = (RECORD_INFO_T*)data_buf;
		RECORD_SEGMENT seg;
		memset(&seg, 0, sizeof(RECORD_SEGMENT));

		seg.diskIndex = pLock->disk;
		seg.indexNO = pLock->blk_no;
		seg.recSegIndex = pLock->seg_no;
		printf("UNLOCK_FILE diskIndex %d indexNO %d recSegIndex %d .\n",
			seg.diskIndex, seg.indexNO, seg.recSegIndex);
		STFS_UnlockRecord(&seg);
		break;
	}
	case FILE_CTL_DELETE:
	{	
		RECORD_INFO_T* pLock = (RECORD_INFO_T*)data_buf;
		RECORD_SEGMENT seg;
		memset(&seg, 0, sizeof(RECORD_SEGMENT));

		seg.diskIndex = pLock->disk;
		seg.indexNO = pLock->blk_no;
		seg.recSegIndex = pLock->seg_no;
		printf("delelte file diskIndex %d indexNO %d recSegIndex %d .\n",
			seg.diskIndex, seg.indexNO, seg.recSegIndex);
		STFS_DeleteRecord(&seg);
		break;
	}
	case FILE_CTL_REQ_FILE_DELETE:
	//	pDelFileLst = (FILE_DEL_INFO_REQ*)data_buf;

	//	ret = m_fInfoManger.DelFile(pDelFileLst);

		break;

	case FILE_CTL_REQ_THUMB_OFFSET:
		TRACE("recive cmd FILE_CTL_REQ_THUMB_OFFSET.\n");
		break;

	case FILE_CTL_REQ_FILE_DATE:
		TRACE("recive cmd FILE_CTL_REQ_FILE_DATE.\n");
		break;

	case FILE_CTL_REQ_FILE_SAVING:

		TRACE("recive cmd FILE_CTL_REQ_FILE_SAVING.\n");
		break;

	case FILE_CTL_REQ_BACKUP:
	{
		typedef struct UploadFileReq
		{
			uint8_t  svr_addr_len;
			char    svr_ip[64];
			uint16_t port;

			uint8_t str_len;
			char     user[32];
			char   passwd[16];
			char   path[64];

			uint8_t ch;
			time_t  start_tm;  //YY-MM-DD-HH-MM-SS，全 0 表示无起始时间条�?
			time_t  end_tm;
			uint32_t disk_no;
			uint32_t file_idx;
			uint64_t alarm_flag;
			uint8_t media_type;
			uint8_t stream_type;
			uint8_t stor_type;
			uint8_t task_type;
		}UploadFileReq_t;
		UploadFileReq_t* ftp = (UploadFileReq_t*)data_buf;
		
		m_fInfoManger.set_file_blk(ftp->disk_no, ftp->file_idx,ftp->start_tm,ftp->end_tm);
		m_fInfoManger.set_ftp_info(ftp->svr_ip, ftp->port, ftp->user, ftp->passwd, ftp->path);

		m_fInfoManger.start_ftp_push();

		#ifdef _HAVE_JT808_
		m_ftp_client.set_file_blk(ftp->disk_no, ftp->file_idx);

		m_ftp_client.init_ftp(ftp->ch, ftp->start_tm, ftp->end_tm,
			ftp->svr_ip, ftp->port, ftp->user, ftp->passwd, ftp->path, 0);

		m_ftp_client.start_ftp_push();
		#endif
		break;
	}
	
	default:
		break;
	}
	#endif
	return ret;
}

int CDVRCmd::CfgGetProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	switch (sub_cmd)
	{
	case CONFIG_GET_RECORD_CFG:
		TRACE("recive cmd CONFIG_GET_RECORD_CFG.\n");
		break;

	case CONFIG_GET_WIFI_CFG:
		TRACE("recive cmd CONFIG_GET_WIFI_CFG.\n");
		break;

	case CONFIG_GET_VERSION:
		TRACE("recive cmd CONFIG_GET_VERSION.\n");
		break;
	default:
		break;
	}
	return ret;
}

int CDVRCmd::CfgSetProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	int runRet = 0;
	switch (sub_cmd)
	{
	case CONFIG_SET_RESOLUTION:

		m_beart.resolution = (uint8_t)data_buf[0];


		ret = RespondDataPkt((char *)&runRet,4);

		break;
	case CONFIG_SET_DURATION:
		m_beart.duration = (uint8_t)data_buf[0];

		ret = RespondDataPkt((char *)&runRet,4);

		break;

	case CONFIG_SET_AUDIO_RECORD:
		m_beart.audioRecStat = (uint8_t)data_buf[0];
		ret = RespondDataPkt((char *)&runRet,4);
		break;
	case CONFIG_SET_OSD:
		//m_beart.AudioRecordStatus = (uint8_t)data_buf[0];
		ret = RespondDataPkt((char *)&runRet,4);
		break;

	case CONFIG_SET_WIFI_CFG:
		TRACE("recive cmd CONFIG_SET_WIFI_CFG.\n");
		break;

	case CONFIG_SET_SDCARD_FMT:
		TRACE("recive cmd CONFIG_SET_SDCARD_FMT.\n");
		break;

	case  CONFIG_SET_FACTORY_RESET:
		TRACE("recive cmd CONFIG_SET_FACTORY_RESET.\n");
		break;

	case CONFIG_SET_SDCARD_UMOUNT:
		TRACE("recive cmd CONFIG_SET_SDCARD_UMOUNT.\n");
		break;

	default:
		break;
	}
	return ret;
}

int CDVRCmd::ReplayCtrlProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	uint32_t ch  = 0;
	int clientID = m_clientID;
	int speed = 0;
	time_t seekTm = 0;
	PLAYBACK_MODE playMod = REPLAY_NORMAL;
	char  fileName[128] = {0};

	//PLAY_INFO_S * playInfo;
	SEEK_INFO_S * seekInfo;
	SEEK_NORMAL_S * seekNomal;
	SEEK_MODE_S * seekMod;
	int runRet      = 0;  //??н??


	PALY_CTRL_S * pCtrl = NULL;

	switch (sub_cmd)
	{
	case START_REPLAY:
		// playInfo = (PLAY_INFO_S*) data_buf;

		// runRet = STFS_PlaybackBytime(playInfo->tm_start,playInfo->tm_end, playInfo->tm_seek,playInfo->channel,m_clientID);
		 
		// ret = RespondDataPkt((char *)&runRet,4);
		// g_stream_server.SwitchPlayMod(m_clientID,true);

		break;
	case  REPLAY_CTRL:
		{   //1078Э?????
			pCtrl = (PALY_CTRL_S*)data_buf;
			ch = pCtrl->ch;
			speed = pCtrl->speed;
			seekTm = pCtrl->date_time;
			TRACE("ch %d contrlType %d  speed %d .\n",
				  ch,pCtrl->contrl_type,speed);
			switch (pCtrl->contrl_type)
			{
			/* 1078
			0����ʼ�طţ�
			1����ͣ�طţ�
			2�������طţ�
			3������طţ�
			4���ؼ�֡���˻طţ�
			5���϶��طţ�
			6���ؼ�֡����
			*/
			case 0: //???????
				playMod = REPLAY_NORMAL;
				runRet = STFS_PlaybackSetMode(playMod,speed,1,ch,m_clientID);
				break;
			case 1: //��ͣ�ط�
				//?�?????????????????????????????????????????á?
				break;
			case 2: //�����ط�
				g_stream_server.stop_stream();
				runRet = STFS_PlaybackStop(ch,m_clientID);
				break;
			case 3: //����ط�
				playMod = REPLAY_FAST;
				runRet = STFS_PlaybackSetMode(playMod,speed,1,ch,m_clientID);
				break;
			case 4: //???????????
				playMod = REPLAY_REBACK;
				runRet = STFS_PlaybackSetMode(playMod,speed,1,ch,m_clientID);
				break;
			case 5: //???????
				playMod = REPLAY_NORMAL;

				runRet = STFS_PlaybackSetMode(playMod,speed,1,ch,m_clientID);

				runRet = STFS_PlaybackSeekTime(seekTm,ch,m_clientID);
				break;
			case 6:
				playMod = REPLAY_FAST;
				speed = 1;
				runRet = STFS_PlaybackSetMode(playMod,speed,1,ch,m_clientID);
				break;
			default:
				break;
			}
			ret = RespondDataPkt((char *)&runRet,4);
		}
		break;
	case SEEK_TIME:
		seekInfo = (SEEK_INFO_S*) data_buf;

		ch = seekInfo->channel;
		seekTm = seekInfo->seek_time;

		ret = STFS_PlaybackSeekTime(seekTm,ch,clientID);

		break;

	case FAST_REPLAY:
		seekMod = (SEEK_MODE_S*) data_buf;

		ch = seekMod->channel;

		playMod = REPLAY_FAST;
		ret = STFS_PlaybackSetMode(playMod,speed,1,ch,clientID);

		seekTm =  seekMod->seek_time;
		if( 0 != seekTm)
		{
			ret = STFS_PlaybackSeekTime(seekTm,ch,clientID);
		}

		break;
	case REBACK_REPLAY:
		seekMod = (SEEK_MODE_S*) data_buf;

		ch = seekMod->channel;

		playMod = REPLAY_REBACK;
		ret = STFS_PlaybackSetMode(playMod,speed,1,ch,clientID);

		seekTm =  seekMod->seek_time;
		if( 0 != seekTm)
		{
			ret = STFS_PlaybackSeekTime(seekTm,ch,clientID);
		}

		break;

	case NORNAL_REPLAY:
		seekNomal = (SEEK_NORMAL_S*) data_buf;

		ch = seekNomal->channel;

		playMod = REPLAY_NORMAL;
		ret = STFS_PlaybackSetMode(playMod,speed,1,ch,clientID);
		seekTm =  seekNomal->seek_time;
		if( 0 != seekTm)
		{
			ret = STFS_PlaybackSeekTime(seekTm,ch,clientID);
		}

		break;

	case STOP_REPLAY:
		ch = *((uint32_t*) data_buf);
		ret = STFS_PlaybackStop(ch,clientID);
		//?л?????????
		//g_dvrApp.SwitchPlayMod(true);

		break;

	default:
		break;
	}



	return ret;
}

int CDVRCmd::LiveCtrlProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	int chbit = 0;
	int runRet = 0;
	switch(sub_cmd)
	{
		//#define SWITCH_MEDIA                    0x00
	case SWITCH_MEDIA:
		break;
	case SEND_CH_BIT :
		chbit = *((int*)data_buf);
		g_stream_server.SetLiveCH(chbit);
		g_stream_server.SwitchPlayMod(m_clientID,false);
		ret = RespondDataPkt((char *)&runRet,4);
		TRACE("Send ch bit %d .\n",chbit);
		break;
	default:
		break;
	}

	return ret;
}
int CDVRCmd::DiskMangerProc(char sub_cmd,uchar_t * data_buf,int data_sz)
{
	int ret = 0;
	int runRet = 0;
	int send_len = 0;
	char buf[SIZE_4KB + 4] = {0};
	int  buf_size = SIZE_4KB;
	char data[20] = {0};
	char * pDiskInfo = NULL;
	uint32_t * pRespondRet = (uint32_t*)data;

	typedef struct _DISK_FORMAT_PARM_
	{
		char diskName[20];
		uint32_t rec_block_sz_MB; //?????С
		uint32_t com_part_sz_MB;  //???????С
		uint32_t rec_index_sz;    //??????????С
		uint32_t pic_part_sz_MB;  //????????С
		uint32_t car_record_sz_MB; //?г?????С

	}DISK_FORMAT_PARM;
	DISK_FORMAT_PARM* pDisk = (DISK_FORMAT_PARM*)data_buf;
	switch(sub_cmd)
	{
	    case DISK_INFO_GET:
			TRACE("recive cmd Get disk info.\n");
			pDiskInfo = buf + 4;
			ret = STFS_GetDiskInfo(pDiskInfo,buf_size,NULL);
			if(ret > 0)
			{   //???????
				TRACE("Get file list. ret %d.\n",ret);
				uint32_t * pRet = (uint32_t*)buf;

				*pRet = ret;
				send_len = ret * sizeof(DiskInfo) + 4;
				ret = RespondDataPkt(buf,send_len);
			}
			else
			{   //???
				ret = RespondDataPkt(data,4);
				TRACE("Get file list fail. ret %d.\n",ret);
			}
			break;
		case DISK_FORMAT:
			if (0 ==strcmp(pDisk->diskName, "/dev/sda"))
			{
				*pRespondRet = -1;

				ret = RespondDataPkt(data, 4);
			}
			else
			{
				ret = STFS_FormatToRecdisk(pDisk->diskName,pDisk->rec_block_sz_MB,pDisk->com_part_sz_MB,0);
				TRACE("ret %d Format disk %s block %d MB com part %d MB \n",
				ret, pDisk->diskName,pDisk->rec_block_sz_MB,pDisk->com_part_sz_MB );
				//???
				*pRespondRet = ret;

				ret = RespondDataPkt(data,4);
			}
			break;
		case  DISK_UMOUNT:
            break;
		case DISK_TEST:
			break;
		default:
			break;
	}

	return ret;
}
int CDVRCmd::HannShake()
{
	int ret = 0;

	return ret;
}

int CDVRCmd::HeadBeat()
{
	int ret = 0;

	return ret;
}

int  CDVRCmd::LuaRunProc(char sub_cmd, uchar_t* data_buf, int data_sz)
{
	int ret = 0;

	switch (sub_cmd)
	{
	// case LUA_RUN_STRING:
	// //	g_lua_app->start_run((char*)data_buf, data_sz);
	// 	break;
	// case LUA_RUN_FILE:
	// 	break;
	default:
		break;
	}

	return ret;
}
